package com.fusion.core.comm.rpc;

import com.fusion.core.comm.Packet;
import com.fusion.core.comm.RPC;
import com.fusion.core.comm.T;
import com.fusion.core.file.share.ShareBase;

import java.io.IOException;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 8, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class GetDirectoryListing extends RPC {
    private String path;
    private int shareBaseIndex;

    public GetDirectoryListing() {
    }

    public GetDirectoryListing(int shareBaseIndex, String path) {
        if(T.t)T.info("About to send GetDirectoryListing. ShareBaseIndex: "+shareBaseIndex+", path: "+path);
        this.shareBaseIndex = shareBaseIndex;
        this.path = path;
    }

    public void execute(Packet data) throws IOException {
        shareBaseIndex = data.readInt();
        String path = data.readUTF();
        if (path.startsWith("/")) path = path.substring(1);
        ShareBase sb = manager.getCore().getShareManager().getBaseByIndex(shareBaseIndex);
        String files[] = manager.getCore().getFileManager().getFileDatabase().getDirectoryListing(sb, path);
        send(new DirectoryListing(shareBaseIndex, path, files));
    }

    public Packet serializeTo(Packet p) {
        p.writeInt(shareBaseIndex);
        p.writeUTF(path);
        return p;
    }
}
